package com.star.langchain.learn.tools;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import com.star.langchain.learn.model.RiskResponse;
import com.star.langchain.learn.model.RiskSearchRequest;
import dev.langchain4j.agent.tool.Tool;

import org.springframework.stereotype.Service;

@Slf4j
@Service
public class BizTools {

    @Tool(name = "根据查询条件获取隐患列表", value = "根据查询条件获取隐患列表,用户语义中没有的条件不要乱造，直接置为空，列表查出来可能是空的，那没问题，因为根据查询条件确实可能为空")
    List<RiskResponse> searchByCondition(RiskSearchRequest searchRequest) {

        log.info("根据查询条件获取隐患列表:{}", searchRequest);

        List<RiskResponse> riskResponses = fakeListRisks();
        if (StringUtils.isNotBlank(searchRequest.getRiskType())) {
            riskResponses = riskResponses.stream()
                    .filter(r -> r.getRiskType().equals(searchRequest.getRiskType())).toList();
        }
        if (StringUtils.isNotBlank(searchRequest.getRiskLevel())) {
            riskResponses = riskResponses.stream()
                    .filter(r -> r.getRiskLevel().equals(searchRequest.getRiskLevel())).toList();
        }
        if (StringUtils.isNotBlank(searchRequest.getRiskSource())) {
            riskResponses = riskResponses.stream()
                    .filter(r -> r.getRiskSource().equals(searchRequest.getRiskSource())).toList();
        }
        if (StringUtils.isNotBlank(searchRequest.getRiskStatus())) {
            riskResponses = riskResponses.stream()
                    .filter(r -> r.getRiskStatus().equals(searchRequest.getRiskStatus())).toList();
        }
        return riskResponses;
    }

    //
    private List<RiskResponse> fakeListRisks() {
        RiskResponse riskResponse1 = new RiskResponse();
        riskResponse1.setRiskType("FIRE");
        riskResponse1.setRiskLevel("HIGH");
        riskResponse1.setRiskSource("MANUAL");
        riskResponse1.setRiskStatus("IN_PROGRESS");
        riskResponse1.setRiskTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        riskResponse1.setRiskLocation("三利广场一楼");
        riskResponse1.setRiskDescription("发现火源，已启动灭火设备");

        RiskResponse riskResponse2 = new RiskResponse();
        riskResponse2.setRiskType("ELECTRICITY");
        riskResponse2.setRiskLevel("MEDIUM");
        riskResponse2.setRiskSource("AUTOMATIC");
        riskResponse2.setRiskStatus("RESOLVED");
        riskResponse2.setRiskTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        riskResponse2.setRiskLocation("三利广场二楼");
        riskResponse2.setRiskDescription("发现异常电流，已关闭电源");

        List<RiskResponse> risks = new ArrayList<>();
        risks.add(riskResponse1);
        risks.add(riskResponse2);
        return risks;
    }

}
